import React, { Component } from 'react';
import { connect } from 'dva';
import { Flex, Icon, Toast } from 'antd-mobile';

import SearchBarMy from '../../components/SearchBar/SearchBar';
import MyScroll from '../../components/MyScroll/MyScroll';
import NavBottomBar from '../../components/NavBottomBar/NavBottomBar';
import TimeBuyBottomBar from '../../components/NavBottomBar/TimeBuyBottomBar';
import RecommendGoods from "../../components/RecommendGood/RecommendGoods";
import { APPSECRECT, publicWXAppID, SERVER_DOMAIN, gotoLogin } from '../../utils/common';
import HomeCarouselBlock from './HomeCarouselBlock';
import HomeFunctionBlock from './HomeFunctionBlock';
import HomeFloorGoods from './HomeFloorGoods';
import HotInformation from './HotInformation';
import genuine from '../../assets/svg/genuine.svg';
import senday from '../../assets/svg/senday.svg';
import rapid from '../../assets/svg/rapid.svg';
import { hoem_footer, go_top } from './Home.less';
import { beLogin } from '../../utils/judgmentEquipment';

class Home extends Component {
    constructor(props) {
        super(props);
        this.AlloyTouch = null;
    }

    componentDidMount() {
        const code = this.getUrlParms("code");
        const type = this.getUrlParms("state");

        // 首页跳转登录
        const username = this.getUrlParms("username");
        const password = this.getUrlParms("password");

        console.warn(type, "welcome to home");
        let that = this;

        if (username && password) {
            this.props.dispatch({ type: 'home/loginEFF', obj: { username, password } }).then((r) => {
                console.log(r);
                if (r.code == 200) {
                    sessionStorage.setItem('token', r.ret.token);
                    localStorage.setItem('token', r.ret.token);
                    beLogin();
                } else {
                    if (r.msg == '验证码错误') this.setState({ num: ++this.state.num });
                    Toast.fail(r.msg, 1.5);
                }
            });
        }


        if (code && type == "weixin") {
            this.props.dispatch({ type: "home/getOpenidEFF", obj: { appid: publicWXAppID, code: code, secret: APPSECRECT } }).then(res => {
                var d = res.ret;
                if (res.code == 200) {
                    that.props.dispatch({ type: "home/thirdLoginEFF", obj: { name: d.userName, loginType: "weixin", openID: d.openid, sex: d.sex, face: d.avatar } }).then(data => {
                        if (data.code == 200) {
                            this.props.dispatch(routerRedux.push({ pathname: `/` }));
                            localStorage.setItem('token', data.ret.token);
                            console.log(localStorage.getItem('token'));
                            sessionStorage.setItem('token', data.ret.token);
                        };
                    })
                }
            });
            // this.props.dispatch({type:"home/thirdLoginEFF", obj:{name: 'd.userName', loginType: "weixin", openID: 'd.openid', sex: 'd.sex', face: 'd.avatar'}})
            // if(this.props.home.getOpenidList!=[]){
            //     this.props.dispatch({type:"home/thirdLoginEFF",obj:{name:"微信用户",loginType:"weixin",openID:this.props.home.getOpenidList[0].openid}}).then(result=>{
            //         if(result.code==200){
            //             sessionStorage.setItem('token', result.ret.token);
            //         }
            //     })
            // }
        } else if (code) {
            this.props.dispatch({ type: "home/thirdSinaEFF", obj: { code: code } }).then(data => {
                if (data.code == 200) {
                    this.props.dispatch(routerRedux.push({ pathname: `/` }));
                    localStorage.setItem('token', data.ret.token);
                    console.log(localStorage.getItem('token'));
                    sessionStorage.setItem('token', data.ret.token);
                };
            })
        }
    }
    getUrlParms = (name) => {
        let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        let r = window.location.search.substr(1).match(reg);
        if (r != null)
            return unescape(r[2]);
        return null;
    }
    getAlloyTouch = (AT) => {
        this.AlloyTouch = AT;
    };

    render() {
        // console.log(this.props)

        const { location, home, dispatch, app } = this.props;
        const { floorList, contentList, menuList, carouselList, list, newNum } = home;
        let { targetScrollHeight } = app;
        //console.log(targetScrollHeight)
        //  console.log(app);
        return (
            <Flex style={{
                height: "100%",
                position: 'relative',
            }} direction="column"
            //justify="between" alignContent="stretch"
            >
                <SearchBarMy dispatch={dispatch} newNum={newNum}></SearchBarMy>
                <MyScroll
                    aa={true}
                    getAlloyTouch={this.getAlloyTouch}
                >
                    <HomeCarouselBlock data={carouselList} />
                    {/*<div style={{ height:"0.2rem", backgroundColor:'#F7F7F7' }}></div>*/}
                    <HomeFunctionBlock menuList={menuList}></HomeFunctionBlock>
                    {/*{*/}
                    {/*contentList.map((v,i,a)=>{*/}
                    {/*return <HotInformation style={ a.length == i+1 ? { paddingBottom:'0.2rem' }:{}} key={i} item={ v }></HotInformation>*/}
                    {/*})*/}
                    {/*}*/}

                    {/*<HotInformation contentList={contentList}></HotInformation>*/}


                    {/*{*/}
                    {/*activityBeenList.map(activityBeen => {*/}
                    {/*return <HomePromotionBlock key={activityBeen.activityTypeValue} data={activityBeen} />;*/}
                    {/*})*/}
                    {/*}*/}
                    {/*<HomeNewGoodsBlock dispatch={this.props.dispatch} data={recommendGoodslist} />*/}
                    {/*<HomeNewGoodsBlock dispatch={this.props.dispatch} data={recommendGoodslist} />*/}
                    {/*<HomeNewGoodsBlock dispatch={this.props.dispatch} data={recommendGoodslist} />*/}
                    {
                        floorList.map((floor, index) => {
                            if (floor) {
                                return (
                                    <div key={index}>
                                        <HomeFloorGoods data={floor} dispatch={dispatch} />
                                        <div style={{ height: "0.2rem", backgroundColor: '#F7F7F7' }}></div>
                                    </div>
                                )
                            } else {
                                return false;
                            }

                        })
                    }
                    {list.length !== 0 ? <RecommendGoods dispatch={dispatch} data={list} style={{ margin: '0rem' }}></RecommendGoods> : ""}

                    {list.length !== 0 ? <div className={hoem_footer}>
                        <Flex justify="around">
                            <Icon type={genuine.id} />
                            <Icon type={rapid.id} />
                            <Icon type={senday.id} />
                        </Flex>
                        <Flex style={{ marginTop: "0.12rem", fontSize: "0.18rem" }} justify="around">
                            <div>正品行货</div>
                            <div>急速配送</div>
                            <div>七天退换</div>
                        </Flex>
                        <Flex style={{ marginTop: "0.08rem", fontSize: "0.16rem" }} justify="around">
                            <div>Genuine Guarantee</div>
                            <div>Rapid Delivery</div>
                            <div>7-Day Free Returns</div>
                        </Flex>
                    </div> : ""}


                </MyScroll>
                {
                    targetScrollHeight < 0 ? <div onClick={() => { this.AlloyTouch.to(0); }} className={go_top}></div> : ''
                }

                <NavBottomBar path={location.pathname}></NavBottomBar>
            </Flex>

        );
    }
}


export default connect(({ app, home }) => ({ app, home }))(Home);
